import routerDef from 'utils/routerDef'
export default {
    namespace: 'common',
    state: {
        menuState: { open: [], selected: [] },
        breadState: { list: [], active: {} }
    },
    effects: {
        /**
         * 设置
         */
        * setMenuState({ payload }, { call, put }) {
            //存入缓存
            let menuState = sessionStorage.getItem('menuState');
            try {
                menuState = JSON.parse(menuState);
            } catch (error) {
                menuState = { open: [], selected: [] }
            }
            menuState = menuState || { open: [], selected: [] };
            let { open = [], selected = [] } = menuState;
            let openStatus = true;
            let openStatusIndex = -1;
            if (payload.open) {
                for (var i = 0; i < open.length; i++) {
                    if (open[i] == payload.open) {
                        openStatusIndex = i;
                        openStatus = false
                        break;
                    }
                }
            } else {
                openStatus = false;
            }
            if (openStatus == true) {
                open.push(payload.open)
            } else {
                if (openStatusIndex > -1) {
                    open.splice(openStatusIndex, 1);
                }
            }
            //不为空的时候再写入
            if (payload.selected) {
                selected = [payload.selected];
            }
            menuState = { open, selected };
            let menuStateString = JSON.stringify({ open, selected });
            sessionStorage.setItem('menuState', menuStateString);
            yield put({
                type: 'updateState',
                payload: { menuState }
            });
        },
        /**
         * 获取
         */
        * getMenuState({ payload }, { call, put }) {
            //从缓存取出并显示
            let menuState = sessionStorage.getItem('menuState');
            try {
                menuState = JSON.parse(menuState);
            } catch (error) {
                menuState = { open: [], selected: [] }
            }
            menuState = menuState || { open: [], selected: [] };
            let localSelected = "";
            if (menuState.selected.length >= 1) {
                localSelected = menuState.selected[0];
            }
            let pathname = window.location.pathname;
            if (pathname != localSelected) {
                if (localSelected != "") {
                    //跳转一次页面
                    routerDef.push(localSelected);
                } else {
                    //设置左侧菜单
                    yield put({
                        type: 'setMenuState',
                        payload: { open: [], selected: [pathname] },
                    });
                }
            }
            yield put({
                type: 'updateState',
                payload: { menuState },
            });
        },
        /**
         * 设置
         */
        * setBreadState({ payload }, { call, put, select }) {
            let breaditem = payload;
            const { breadState } = yield select(_ => _.common);
            let breadStateTemp = JSON.parse(JSON.stringify(breadState));
            let { list = [], active = {} } = breadStateTemp;
            let pushStatus = true;
            for (var i = 0; i < list.length; i++) {
                if (breaditem.url == list[i].url) {
                    active = list[i];
                    pushStatus = false;
                    break;
                }
            }

            if (pushStatus == true) {
                if (breaditem.isIndex != true) {
                    list.push(breaditem);
                }

                active = breaditem;
            }
            breadStateTemp = { list, active }
            let breadStateString = JSON.stringify(breadStateTemp);
            sessionStorage.setItem('breadState', breadStateString);
            yield put({
                type: 'updateState',
                payload: { breadState: breadStateTemp },
            });
        },
        /**
        * 获取
        */
        * delBreadByIndex({ payload }, { call, put, select }) {
            const { breadState } = yield select(_ => _.common);
            let index = payload.index;
            console.log("删除===", index);
            let breadStateTemp = JSON.parse(JSON.stringify(breadState));
            let { list = [], active = {} } = breadStateTemp;
            let new_active = active;
            if (active.url == list[index].url) {
                let bsize = list.length;
                let lastIndex = list.length - 1;
                if (lastIndex == index && bsize > 1) {
                    new_active = list[index - 1]
                } else if (lastIndex > index) {
                    new_active = list[index + 1]
                } if (lastIndex == index && bsize == 1) {
                    new_active = {
                        url: "/",
                        isIndex: true
                    }
                }
            }
            if (new_active.url != active.url) {
                routerDef.push(new_active.url)
            }
            list.splice(index, 1);
            let saveBreadStateTemp = { list, active: new_active };
            let breadStateString = JSON.stringify(saveBreadStateTemp);
            sessionStorage.setItem('breadState', breadStateString);
            yield put({
                type: 'updateState',
                payload: { breadState: saveBreadStateTemp },
            });
        },
        /**
         * 获取
         */
        * getBreadState({ payload }, { call, put }) {
            //从缓存取出并显示
            let breadState = sessionStorage.getItem('breadState');
            try {
                breadState = JSON.parse(breadState);
            } catch (error) {
                breadState = { open: [], selected: [] }
            }
            breadState = breadState || { list: [], active: {} };
            
            yield put({
                type: 'updateState',
                payload: { breadState },
            });
        }

    },

    reducers: {
        updateState(state, { payload }) {
            return {
                ...state,
                ...payload,
            }
        }
    }
}